﻿window.onload = loginLoad;

function loginLoad() {

    //是否有登录提示
   // var msg = $.cookie('loginmsg');
    //var msg = "abc";
   // if (msg) $('#logintooltip').html(msg);
	
//	init();

    //登录按钮
    $('#btn-login').on('click', function () {
        var name = $('#username').val();
        if (name == '') {
            $('#username').focus();
            $('#logintooltip').html("请输入用户名");
            return;
        }
        var pwd = $('#userpwd').val();
        if (pwd == '') {
            $('#userpwd').focus();
            $('#logintooltip').html("请输入密码");
            return;
        }
        //$('#userpwd').val(hex_md5(pwd));
        login();
    });

    $('#username').on('keypress', function (e) {
        if (e.charCode == 13) {
            var name = $('#username').val();
            if (name == '') {
                $('#username').focus();
                $('#logintooltip').html("请输入用户名");
            } else {
                $('#userpwd').val('');
                $('#userpwd').focus();
            }
        }
    });

    $('#userpwd').on('keypress', function (e) {
        if (e.charCode == 13) {
            var name = $('#username').val();
            if (name == '') {
                $('#username').focus();
            } else {
                var pwd = $('#userpwd').val();
                if (pwd == '') {
                    $('#userpwd').focus();
                    $('#logintooltip').html("请输入密码");
                } else {
//                    $('#btn-login').click();
                	login();
                }
            }
        }
    });
}


function onlogin(){
    var name = $('#username').val();
    if (name == '') {
        $('#username').focus();
        $('#logintooltip').html("请输入用户名");
        return;
    }
    var pwd = $('#userpwd').val();
    if (pwd == '') {
        $('#userpwd').focus();
        $('#logintooltip').html("请输入密码");
        return;
    }
    //$('#userpwd').val(hex_md5(pwd));
    login();
}

function login(){

    
   
    var sUrl="client/service/loginController/queryLoginUser";
    var sIndexUrl="client/index.jsp";
   /* 
    if (userType=="5" ||userType=="普通用户"){
        sUrl="client/service/clientUserController/queryAdminUser";
        sIndexUrl="client/index.jsp";
    }*/
    
//    loginStatus();
    $.ajax({
        url: sUrl,
        data: 'username=' + $("#username").val() + "&password=" + $("#userpwd").val()+"&loginCheckbox="+document.getElementById("loginCheckbox").checked,
        type: 'post',
        dataType: 'json',
        success: function(msg){
        	if(msg.result == "success"){ 
        		location.href=sIndexUrl; 
           	}else if(msg.result == "wrongIn"){
        		$('#logintooltip').html("调用登录接口失败,请重试!");
        		$('#userpwd').focus();
        	}else{
        		$('#logintooltip').html("用户名或密码错误");
        		$('#userpwd').focus();
        	}
        	
        },
        error: function(){
        	$('#logintooltip').html("登录异常");
        }
    });
}


function init(){
	//初始化账号密码
    $.ajax({
        url: "client/service/loginController/initLoginUserName",
        data: 'username=' + $("#username").val() + "&password=" + $("#userpwd").val()+"&loginCheckbox="+
        $('input[name="loginCheckbox"]:checked').length,
        type: 'post',
        dataType: 'json',
        success: function(msg){
        	if(msg.success == true&& msg.result!= undefined){
        		var datas = msg.result.split("-_-");
        		document.getElementById('username').value = datas[0];
        		document.getElementById('userpwd').value = datas[1];
        		document.getElementById("loginCheckbox").checked = true;
           	}
        },
        error: function(){
        	var error=document.getElementById('login_error');
        	error.innerText='用户名或密码不正确，请重新输入';
        	error.style.display='block';
            $("#username").focus();
	        loginStatusReset();
        }
    });
	
	//网页内按下回车触发
	document.onkeyup=function(){
	    if(event.keyCode==13)
	    {
	    	login();
	        return false;                               
	    }
	}
}

